//
//  NewSportNetworking.m
//  SportRing
//
//  Created by 微我网络 on 15/1/30.
//  Copyright (c) 2015年 HotOne. All rights reserved.
//

#import "NewSportNetworking.h"
#import "SRMe.h"
#import "SRMe+Cate.h"

#import <NSDate+DateTools.h>
#import "NSDate+Ex.h"
#import "NSDate+SR.h"
#import "NSDate+Additionals.h"

@implementation NewSportNetworking

+ (void)getStatisticsByMonthCounterWithToken:(NSString *)token
                                       month:(NSString *)month
                                     success:(CompletionBlockWithDictionary)success
                                     failure:(CompletionBlockWithError)failure
{
    [self postAction:@"v1/Counter/GetStatisticsByMonth"
          parameters:@{@"token":token, @"month":month}
             success:success
             failure:failure];
}

+ (void)getRecordWithDate:(NSString *)date success:(CompletionBlockWithArray)success fail:(CompletionBlockWithError)fail {
    
    [self postAction:@"v1/counter/getrecord"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"date":date}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];
}

+ (void)getTaskWithDate:(NSString *)date success:(CompletionBlockWithDictionary)success fail:(CompletionBlockWithError)fail {
    
    [self postAction:@"v1/task/gettask"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"date":date}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];
}

+ (void)getRecordsWithStart:(NSString *)start end:(NSString *)end success:(CompletionBlockWithArray)success fail:(CompletionBlockWithError)fail {
    
//    NSDate *now  = [NSDate date];
//    NSDate *date = [NSDate dateFromString:yyyyMM format:@"yyyy-MM"];
//    
//    NSString *startDate,*endDate;
//    if (now.month == date.month) {
//        startDate = [[now dateByMovingToFirstDayOfTheMonth] formattedBy_yyyyMMdd];
//        endDate = [[now dateBySubtractingDays:1] formattedBy_yyyyMMdd];
//    }
//    else {
//        startDate = [[date dateByMovingToFirstDayOfTheMonth] formattedBy_yyyyMMdd];
//        endDate = [[[[date dateByMovingToFirstDayOfTheMonth] dateByAddingMonths:1] dateBySubtractingDays:1] formattedBy_yyyyMMdd];
//    }
    
    [self postAction:@"v1/counter/getrecords"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"startDate":start,
                       @"endDate":end}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];
}

+ (void)getTasksWithStart:(NSString *)start end:(NSString *)end success:(CompletionBlockWithArray)success fail:(CompletionBlockWithError)fail {
    
//    NSDate *now  = [NSDate date];
//    NSDate *date = [NSDate dateFromString:yyyyMM format:@"yyyy-MM"];
//    
//    NSString *startDate,*endDate;
//    if (now.month == date.month) {
//        startDate = [[now dateByMovingToFirstDayOfTheMonth] formattedBy_yyyyMMdd];
//        endDate = [[now dateBySubtractingDays:1] formattedBy_yyyyMMdd];
//    }
//    else {
//        startDate = [[date dateByMovingToFirstDayOfTheMonth] formattedBy_yyyyMMdd];
//        endDate = [[[[date dateByMovingToFirstDayOfTheMonth] dateByAddingMonths:1] dateBySubtractingDays:1] formattedBy_yyyyMMdd];
//    }
    [self postAction:@"v1/task/gettasks"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"startDate":start,
                       @"endDate":end}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];
}

+ (void)getWeekRecordsWithStart:(NSString *)start end:(NSString *)end success:(CompletionBlockWithArray)success fail:(CompletionBlockWithError)fail {
    
    [self postAction:@"v1/counter/getrecords"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"startDate":start,
                       @"endDate":end}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];
}

+ (void)getSleepRecordsWithStart:(NSString *)start end:(NSString *)end success:(CompletionBlockWithArray)success fail:(CompletionBlockWithError)fail {
    
    [self postAction:@"v1/sleep/getrecords"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"startDate":start,
                       @"endDate":end}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];
}

+ (void)getSleepRecordWithDate:(NSString *)date success:(CompletionBlockWithDictionary)success fail:(CompletionBlockWithError)fail {
    
    [self postAction:@"v1/sleep/getrecord"
          parameters:@{@"token":[SRMe currentUser].token,
                       @"date":date}
             success:^(id obj) {
                 if ([obj[@"code"] integerValue] == 0) {
                     success(obj[@"data"]);
                 }
             } failure:^(NSError *error) {
                 fail(error);
             }];

}

@end
